package helixoide;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

import helixoide.hxLog;
import helixoide.secondlife.devices.device;
import helixoide.secondlife.devices.deviceManager;
/*
 * Util class has various utilities to manage the system
 * at the moment it only has utilities to manage threads
 */
public class Util 
{
	public static ArrayList<Thread> threads;
	private static String deviceData = "";
	public static final String backupFile = "slirc.bac";//this is the filename for the backup file
	private static final String[] illegalNickChars = {"/","+",":",";","%","$","#","@","!","&","*","=",",",".","?","\"","'","(",")"};
	private static boolean isClean(String s)
	{
		boolean clean = true;
		for(int i = 0;i<s.length();i++)
		{
			for(int k = 0;k<Util.illegalNickChars.length;k++)
			{
				if(s.substring(i, i+1).equals(Util.illegalNickChars[k]))
				{
					clean = false;
					break;
				}
			}
		}
		return clean;
	}
	public static String sanitize(String start)
	{
		String temp = "";
		String end = "";
		for(int i = 0;i<start.length();i++)
		{
			temp = start.substring(i, i+1);
			if(!Util.isClean(temp))
			{
				temp = "";
			}
			end += temp;
			
		}
		return end;
	}
	public static Thread startThread(Thread toStart, String type)
	{
		hxLog.write("Utilities", "Starting " + toStart.getName() + " " + type);
		if(threads==null)
			threads = new ArrayList<Thread>();
		toStart.start();
		threads.add(toStart);
		
		return toStart;
	}
	public static ArrayList<Thread> getThreads()
	{
		return threads;
	}
	public static void interrupt(Thread toKill)
	{
		hxLog.write("Utilities", "removing thread: " + toKill.getName());
		threads.remove(threads.indexOf(toKill));
	}
	public static void interruptAll()
	{
		hxLog.write("Utilities", "removing all Threads");
		for(Thread t : threads)
		{
			t.interrupt();
			Util.interrupt(t);
		}
	}
	/*
	 * TODO:
	 * make this method dump directly to a text file
	 * eventually there will also be a load method which loads devices from this text file
	 */
	public static void dumpDeviceData(deviceManager manager)
	{
		
		String dataDump = "";
		String dumpSep = "&&";
		device[] theDevices = manager.getDevices();
		for(int i = 0;i<theDevices.length;i++)
		{
			if(i==theDevices.length - 1)
			{
				dataDump += theDevices[i].dump();
			}
			else
			{
				dataDump += theDevices[i].dump() + dumpSep;
			}
		}
		Util.deviceData = dataDump;
		try
		{
			FileOutputStream erasor = new FileOutputStream(Util.backupFile);
    		String clear = new String();
    		erasor.write(clear.getBytes());
    		erasor.close();
			FileWriter aWriter = new FileWriter(Util.backupFile, true);
			aWriter.write(Util.deviceData);
			aWriter.flush();
			aWriter.close();
		}
		catch(Exception e)
		{
			hxLog.write("Backup", "Error writing to backup file");
			e.printStackTrace();
		}
		
	}
	/*
	 * @TODO
	 * This method loads a device data dump file
	 * needs to be implmented
	 */
	public static void loadDeviceData(deviceManager manager)
	{
		hxLog.write("Backup Loader", "Loading Backup File");
		String theFile = "";
		File toLoad = new File(Util.backupFile);
		if(toLoad.exists())
		{
			try
			{
				Scanner fileIn = new Scanner(toLoad);
				while(fileIn.hasNext())
				{
					theFile += fileIn.next();
				}
			}
			catch(IOException e)
			{
				e.printStackTrace();
				hxLog.write("Backup Loader", "IOException Caught");
			}
			if(!theFile.equals("") && !theFile.equals(null) && theFile.indexOf("@@")!=-1)
			{
				String[] device = theFile.split("&&");
				String[] deviceInfo = null;
				//uuid + "@@" + region + "@@" + location + "@@" + channel + "@@" + urlString
				for(int i = 0;i<device.length;i++)
				{
					deviceInfo = device[i].split("@@");
					hxLog.write("Device Loader","Device for channel " + deviceInfo[3] + " has been loaded from backup");
					manager.register(deviceInfo[0].trim(), deviceInfo[2].trim(), deviceInfo[1].trim(), deviceInfo[3].trim(), deviceInfo[4].trim(), true);
				}
			}
		}
		else
		{
			hxLog.write("Backup Loader","backup file does not exist, loading discontinued");
		}
			
	}
}
